package petstore.web.servlet.catalog;

import petstore.domain.Category;
import petstore.domain.Product;
import petstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class ProductServlet extends HttpServlet
{
    private String PRODUCT_PAGE="/WEB-INF/jsp/catalogPage/product.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryId = req.getParameter("categoryId");//获取从request过来的categoryId
        CatalogService catalogService = new CatalogService();//实例化一个CatalogService对象
        Category category = catalogService.getCategory(categoryId);//利用获取的categoryId，获取category相关信息
        List<Product> productList = catalogService.getProductListBycategoryId(categoryId);//利用获取的categoryId，获取所有Product相关信息
        //向jsp传递
        HttpSession session = req.getSession();
        session.setAttribute("category",category);
        session.setAttribute("productList",productList);
        req.getRequestDispatcher(PRODUCT_PAGE).forward(req,resp);
    }
}
